package com.sugon.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.RandomUtil;
import com.sugon.dao.TicketLiveImageDao;
import com.sugon.entity.TicketLiveImageEntity;
import com.sugon.service.TicketLiveImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 工单现场照片信息Service实现类
 *
 * @author YuXD
 * @date 2020-12-18 14:07
 */
@Service("ticketLiveImageService")
public class TicketLiveImageServiceImpl implements TicketLiveImageService {

    @Autowired
    private TicketLiveImageDao ticketLiveImageDao;

    /**
     * @param ticketId 工单ID
     * @param path     图片存储路径
     * @return
     */
    @Override
    public int addRepairTicketLiveImage(String ticketId, String path) {
        return addTicketLiveImage(ticketId, "REPAIR", path);
    }

    @Override
    public boolean batchUpdateTicketImages(String ticketId, String ticketType, String groupName, List<String> paths) {
        // 移除工单现有照片
        ticketLiveImageDao.deleteByTicket(groupName, ticketId, ticketType);
        // 如果有，则保存照片
        if (CollectionUtil.isNotEmpty(paths)) {
            for (String path : paths) {
                addTicketLiveImage(ticketId, ticketType, groupName, path);
            }
        }
        return true;
    }

    /**
     * @param ticketId 工单ID
     * @param path     图片存储路径
     * @return
     */
    @Override
    public int addMaintenanceTicketLiveImage(String ticketId, String path) {
        return addTicketLiveImage(ticketId, "MAINTENANCE", path);
    }

    @Override
    public List<String> getTicketLiveImages(String groupName, String ticketId, String... ticketType) {
        // 转化成集合操作
        List<String> ticketTypeList = CollectionUtil.newArrayList(ticketType);
        List<String> ticketLiveImages = ticketLiveImageDao.getTicketLiveImages(groupName, ticketId, ticketTypeList);
        return Optional.ofNullable(ticketLiveImages).orElse(CollectionUtil.newArrayList());
    }

    @Override
    public List<TicketLiveImageEntity> queryList(String ticketId, String ticketType) {
        Map<String, Object> queryParam = MapUtil.newHashMap();
        queryParam.put("ticketId", ticketId);
        queryParam.put("ticketType", ticketType);
        return queryList(queryParam);
    }

    @Override
    public int addTicketLiveImage(String ticketId, String ticketType, String path) {
        return addTicketLiveImage(ticketId, ticketType, null, path);
    }

    @Override
    public int addTicketLiveImage(String ticketId, String ticketType, String groupName, String path) {
        TicketLiveImageEntity ticketLiveImage = new TicketLiveImageEntity();
        ticketLiveImage.setTicketId(ticketId);
        ticketLiveImage.setTicketType(ticketType);
        ticketLiveImage.setGroupName(groupName);
        ticketLiveImage.setPath(path);
        return save(ticketLiveImage);
    }

    /**
     * @param id 主键
     * @return
     */
    @Override
    public TicketLiveImageEntity queryObject(String id) {
        return ticketLiveImageDao.queryObject(id);
    }

    /**
     * @param map 参数
     * @return
     */
    @Override
    public List<TicketLiveImageEntity> queryList(Map<String, Object> map) {
        return ticketLiveImageDao.queryList(map);
    }

    /**
     * @param map 参数
     * @return
     */
    @Override
    public int queryTotal(Map<String, Object> map) {
        return ticketLiveImageDao.queryTotal(map);
    }

    /**
     * @param ticketLiveImage 实体
     * @return
     */
    @Override
    public int save(TicketLiveImageEntity ticketLiveImage) {
        ticketLiveImage.setId(RandomUtil.randomString(32));
        ticketLiveImage.setGmtCreate(DateUtil.date());
        return ticketLiveImageDao.save(ticketLiveImage);
    }

    /**
     * @param ticketLiveImage 实体
     * @return
     */
    @Override
    public int update(TicketLiveImageEntity ticketLiveImage) {
        return ticketLiveImageDao.update(ticketLiveImage);
    }

    /**
     * @param id
     * @return
     */
    @Override
    public int delete(String id) {
        return ticketLiveImageDao.delete(id);
    }

    /**
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(String[] ids) {
        return ticketLiveImageDao.deleteBatch(ids);
    }
}
